/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 *
 * @author SONY
 */
public class MemberDAO {
    
    DataSource ds;
    
    public MemberDAO() {
        try {
            Context context = new InitialContext();
            ds = (DataSource) context.lookup("java:comp/env/jdbc/FashionShopDatasource");
        } catch (Exception e) {
            System.err.println(e);
        }
    }
    
    public int insert (String firstname, String lastname, String address, String phone, String gender) {
        try {
            Connection conn = ds.getConnection();
            String sql = "INSERT INTO Member ([FirstName],[LastName],[Address],[Phone],[Gender]) VALUES(?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pstmt.setNString(1, firstname);
            pstmt.setNString(2, lastname);
            pstmt.setNString(3, address);
            pstmt.setNString(4, phone);
            pstmt.setNString(5, gender);
            if (pstmt.executeUpdate() > 0) {
                ResultSet rs = pstmt.getGeneratedKeys();
                rs.next();
                return rs.getInt(1); //get MemberID just inserted
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return -1;
    }
    
    public Member search_byMemberID(int memberID) {
        try {
            Connection conn = ds.getConnection();
            String sql = "SELECT * FROM Member WHERE MemberID=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, memberID);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                String firstName = rs.getNString("FirstName");
                String lastName = rs.getNString("LastName");
                String address = rs.getNString("Address");
                String phone = rs.getNString("Phone");
                String gender = rs.getNString("Gender");
                Member member = new Member(memberID, firstName, lastName, address, phone, gender);
                return member;
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return null;
    }
    
    public boolean update (int MemberID, String FirstName, String LastName, String Address, String Phone, String Gender) {
        try {
            Connection conn = ds.getConnection();
            String sql = "UPDATE Member SET FirstName=?,LastName=?,Address=?,Phone=?,Gender=?  WHERE MemberID=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setNString(1, FirstName);
            pstmt.setNString(2, LastName);
            pstmt.setNString(3, Address);
            pstmt.setNString(4, Phone);
            pstmt.setNString(5, Gender);
            pstmt.setInt(6, MemberID);
            if (pstmt.executeUpdate() > 0) {
                return true;
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }
}
